package io.zhengsh.vvip.leetcode.hash;

import java.util.*;

public class Q49 {

    class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            if (strs.length == 0) {
                return new ArrayList<>();
            }

            Map<String, List<String>> ans = new HashMap<>();
            for (String s : strs) {
                char[] ca = s.toCharArray();
                Arrays.sort(ca);
                String key = String.valueOf(ca);
                List<String> list = ans.getOrDefault(key, new ArrayList<>());
                list.add(s);
                ans.put(key, list);
            }
            return new ArrayList<List<String>>(ans.values());
        }
    }
}
